const app = getApp()

var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes();


Page({
  data: {
    globalData: app.globalData,
    globalShareCarData: app.globalData.shareCar,

    //时间属性
    nowYear: '',
    startMonthDay: '',
    startHM: '',
    endMonthDay: '',
    endHM: '',

    //费用
    cost: '',
    costMsg: '',

    //用户 && 订单
    userInfo: {},
    orderInfo: {},
    //共享车 
    shareCar: {
      cartype: {},
      meetingBackCarPlacelist: {},
      meetingGetCarPlacelist: {},
    },
  },
  // -------------默认页面时间 -------------
  onLoad: function(options) {
    var that = this;

    //验证登录，如果当前全局没有值，读取setStrong，如果还没有，就去登录页面
    // app.userLogin();

    //获取用户数据缓存
    wx.getStorage({
      key: 'users',
      success: function(e) {
        app.globalData.userInfo = e.data;
        that.setData({
          userInfo: e.data,
          globalData: app.globalData,
        })
      }
    })

    let sID = options.sID;
    let ctypeID = options.ctypeID
    let sLat = options.slat;
    let sLng = options.slng;
    let CID = options.cID;
    this.data.curIndex = CID;

    // 请求初始数据
    app.request({
      url: "/getInitData",
      success: (res) => {
        console.log('--请求共享车数据--', res.data.data)
        this.setData({
          shareCar: {
            cartype: res.data.data.cartype,
            meetingBackCarPlacelist: res.data.data.meetingBackCarPlacelist,
            meetingGetCarPlacelist: res.data.data.meetingGetCarPlacelist,
            cartypeLabel: res.data.data.cartype[0].label,
            cartypeValue: res.data.data.cartype[0].value,
            cartypeRemarks: res.data.data.cartype[0].remarks,
            BackCar: res.data.data.meetingBackCarPlacelist[0].value,
            GetCar: res.data.data.meetingGetCarPlacelist[0].value,
          },
        })
      }
    })
  },


  //------------- 共享汽车 -----------------
  // 取车点
  shareStartShop(item) {
    var that = this;
    that.data.shareCar.GetCar = that.data.shareCar.meetingGetCarPlacelist[parseInt(item.detail.value)].value
    that.setData({
      shareCar: that.data.shareCar
    })
  },
  // 还车点
  shareEndShop(item) {
    var that = this;
    that.data.shareCar.BackCar = that.data.shareCar.meetingBackCarPlacelist[parseInt(item.detail.value)].value
    that.setData({
      shareCar: that.data.shareCar
    })
  },
  // 车型
  shareCarType(item) {
    var that = this;
    if (app.globalData.startDate.monthDay != '' && app.globalData.startDate.hours != '' && app.globalData.startDate.minute != '' && app.globalData.endDate.monthDay != '' && app.globalData.endDate.hours != '' && app.globalData.endDate.minute != '') {

      that.data.shareCar.cartypeValue = that.data.shareCar.cartype[parseInt(item.detail.value)].value
      that.data.shareCar.cartypeLabel = that.data.shareCar.cartype[parseInt(item.detail.value)].label
      that.data.shareCar.cartypeRemarks = that.data.shareCar.cartype[parseInt(item.detail.value)].remarks

      that.setData({
        shareCar: that.data.shareCar
      })

      that.calculationFee();

    } else {
      wx.showToast({
        title: '请确定开始结束时间',
        icon: 'none',
        mask: true,
        duration: 2000
      })
    }
  },
  //清除地图信息
  ClearMapGPS: function() {
    var that = this
    app.globalData.map.endAddress = '目的地';
    app.globalData.map.endLatitude = '';
    app.globalData.map.endLongitude = '';
    app.globalData.startDate = [];
    app.globalData.endDate = [];
    that.setData({
      markers: {},
      globalData: app.globalData,
      globalMapData: app.globalData.map,
    });
  },

  //--------时间选择器----------
  // 回调
  bindStartMultiPickerChange(time) {
    var that = this;
    if (time.currentTarget.dataset.lx == 'start') {
      //判断开始时间有没有值，没有必须先选开始
      if (JSON.stringify(app.globalData.startDate) == "[]" || JSON.stringify(app.globalData.startDate) != "[]") {
        if (JSON.stringify(app.globalData.endDate) != "[]") {
          var strT = new Date(time.detail[0].monthDay + ' ' + time.detail[0].hours + ':' + time.detail[0].minute).getTime();
          var endT = new Date(app.globalData.endDate.monthDay + ' ' + app.globalData.endDate.hours + ':' + app.globalData.endDate.minute).getTime();
          console.log(strT, endT)

          if (strT >= endT) {
            wx.showToast({
              title: '开始时间必须小于结束时间',
              icon: 'none',
              mask: true,
              duration: 2000
            })
          } else {
            var strT2 = new Date(time.detail[0].monthDay).getTime();
            var endT2 = new Date(app.globalData.endDate.monthDay).getTime();
            if (strT2 == endT2) {
              app.globalData.startDate = time.detail[0]
              this.setData({
                globalData: app.globalData,
              })
            } else {
              wx.showToast({
                title: '开始和结束必须是当天',
                icon: 'none',
                mask: true,
                duration: 2000
              })

            }

          }
        } else {
          app.globalData.startDate = time.detail[0]
          this.setData({
            globalData: app.globalData,
          })
        }
      } else {
        if (JSON.stringify(app.globalData.endDate) != "[]") {
          var strT = new Date(app.globalData.startDate.monthDay + ' ' + app.globalData.startDate.hours + ':' + app.globalData.startDate.minute).getTime();
          var endT = new Date(time.detail[0].monthDay + ' ' + time.detail[0].hours + ':' + time.detail[0].minute).getTime();
          //console.log(time.detail[0].monthDay + ' ' + time.detail[0].hours + ':' + time.detail[0].minute, app.globalData.startDate.monthDay + ' ' + app.globalData.startDate.hours + ':' + app.globalData.startDate.minute)
          //console.log(strT, endT)
          if (strT >= endT) {
            wx.showToast({
              title: '开始时间必须小于结束时间',
              icon: 'none',
              mask: true,
              duration: 2000
            })
          } else {
            app.globalData.startDate = time.detail[0]
            this.setData({
              globalData: app.globalData,
            })
          }
        }
      }
      //计算费用
      that.calculationFee();
    } else {
      //判断开始时间有没有值，没有必须先选开始
      if (JSON.stringify(app.globalData.startDate) == "[]") {
        wx.showToast({
          title: '请选择开始时间',
          icon: 'none',
          mask: true,
          duration: 2000
        })
      } else {

        var strT = new Date(app.globalData.startDate.monthDay + ' ' + app.globalData.startDate.hours + ':' + app.globalData.startDate.minute).getTime();
        var endT = new Date(time.detail[0].monthDay + ' ' + time.detail[0].hours + ':' + time.detail[0].minute).getTime();

        var strT2 = new Date(app.globalData.startDate.monthDay).getTime();
        var endT2 = new Date(time.detail[0].monthDay).getTime();

        console.log(strT2, endT2);

        if (endT <= strT) {
          wx.showToast({
            title: '结束时间必须大于开始时间',
            icon: 'none',
            mask: true,
            duration: 2000
          })
          return false;
        }

        if (endT2 == strT2) {
          app.globalData.endDate = time.detail[0]
          this.setData({
            globalData: app.globalData,
          })
        } else {
          app.globalData.endDate = [];
          this.setData({
            globalData: app.globalData,
          })
          wx.showToast({
            title: '结束时间必须是当天的时间',
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
      //计算费用
      that.calculationFee();
    }
  },
  //费用计算
  calculationFee: function() {
    //console.log('calculationFee-----')
    var that = this;
    var carT = that.data.shareCar.cartypeValue
    var carS = app.globalData.startDate.monthDay + ' ' + app.globalData.startDate.hours + ':' + app.globalData.startDate.minute
    var carE = app.globalData.endDate.monthDay + ' ' + app.globalData.endDate.hours + ':' + app.globalData.endDate.minute

    //判断字符是否为空的方法
    function isEmpty(obj) {
      if (typeof(obj) == "undefined" || obj == null || obj == "") {
        return true;
      } else {
        return false;
      }
    }

    if (new Date(carS).getTime() >= new Date(carE).getTime()) {
      //console.log('开始大于结束，false')
      that.setData({
        cost: '',
        costMsg: ''
      })
      return false;
    }

    if (!isEmpty(that.data.shareCar.cartypeValue) && !isEmpty(app.globalData.startDate.monthDay) && !isEmpty(app.globalData.startDate.hours) && !isEmpty(app.globalData.startDate.minute) && !isEmpty(app.globalData.endDate.monthDay) && !isEmpty(app.globalData.endDate.hours) && !isEmpty(app.globalData.endDate.minute)) {

      app.request({
        url: "/calculationFee",
        data: {
          carType: carT,
          dordergetDate: carS,
          dorderbackDate: carE,
        },
        success: (res) => {
          console.log('费用返回成功', res)
          that.setData({
            cost: parseInt(res.data.data),
            costMsg: res.data.msg
          })
        },
        fail: (res) => {
          console.log('请求失败'.res)
        }
      })
    } else {
      console.log('false')
    }
  },
  //订单事件处理
  shareCarForm: function(event) {
    var that = this;
    var orderInfo = event.detail.value;
    orderInfo.openId = app.openId;
    orderInfo.name = app.globalData.userInfo.name;
    orderInfo.storeId = app.globalData.userInfo.storeId;

    if (orderInfo.phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(orderInfo.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }

    if (orderInfo.dordergetDate == '- ' || orderInfo.dorderbackDate == '- ' || orderInfo.dordergetDate == undefined || orderInfo.dorderbackDate == undefined || orderInfo.backPlace == '' || orderInfo.getPlace == '' || orderInfo.name == '' || orderInfo.openId == '') {
      console.log('共享车-失败---', orderInfo)
      wx.showToast({
        title: '请确认信息完整',
        icon: 'none',
        mask: true,
        duration: 2000
      })
    } else {

      // var strT = new Date(app.globalData.startDate.monthDay).getTime();
      // var endT = new Date(app.globalData.endDate.monthDay).getTime();

      // if (strT > endT || strT < endT) {
      //   wx.showToast({
      //     title: '时间选择出错！',
      //     icon: 'none',
      //     mask: true,
      //     duration: 2000
      //   })
      //   return false
      // }

      console.log('共享车-成功---', orderInfo)
      app.request({
        url: "/reserveCarSave",
        data: orderInfo,
        success: function(res) {
          wx.showToast({
            title: '提交订单成功',
            icon: 'success',
            duration: 2000,
            mask: true,
            success: function() {
              setTimeout(function() {
                wx.reLaunch({
                  url: '/pages/order/order'
                })
              }, 1800);
            }
          })
        }
      })
    }
    // }
    // }
  },
  onShareAppMessage: function(ops) {
    return {
      title: '广丰出行邀请您一起体验',
      path: 'pages/index/index',
      success: function(res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function(res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },

})